
/**
 * @Title: DemoConfig.java
 * @Package com.yijvyan.demo
 * @Description: TODO(1.jfinal在undertow下开发；)
 * @author Administrator
 * @date 2020-7-30 17:22:10
 * @version V1.0
 */

package com.yijvyan.demo;

import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.server.undertow.UndertowServer;
import com.jfinal.template.Engine;

/**
 * @ClassName: DemoConfig
 * @Description: TODO(2.jfinal的启动文件；)
 * @author Administrator
 * @date 2020-7-30
 *
 */

public class DemoConfig extends JFinalConfig {
    /**
     * 注意：用于启动的 main 方法可以在任意 java 类中创建，在此仅为方便演示 才将 main 方法放在了 DemoConfig 中
     *
     * 开发项目时，建议新建一个 App.java 或者 Start.java 这样的专用 启动入口类放置用于启动的 main 方法
     */
    public static void main(String[] args) {
        // UndertowServer.start(DemoConfig.class, 80, true);

        /*UndertowServer.create(DemoConfig.class).onStart(builder -> {
            builder.setServerOption(UndertowOptions.ALLOW_UNESCAPED_CHARACTERS_IN_URL, true);
        }).start();*/

        /*UndertowServer.create(DemoConfig.class).configWeb(builder -> {
            // 12.2.1.配置Filter；
            builder.addFilter("myFilter", "com.abc.MyFilter");
            builder.addFilterUrlMapping("myFilter", "/*");
            builder.addFilterInitParam("myFilter", "key", "value");
        
            // 12.2.2.配置Servlet；
            builder.addServlet("myServlet", "com.abc.MyServlet");
            builder.addServletMapping("myServlet", "*.do");
            builder.addServletInitParam("myServlet", "key", "value");
        
            // 12.2.3.配置Listener；
            builder.addListener("com.abc.MyListener");
        
            // 12.2.4.配置 WebSocket,MyWebSocket 需要使用 ServletEndpoint 注解；
            builder.addWebSocketEndpoint("com.yijvyan.websocket.MyWebSocket");
        }).start();*/

        UndertowServer.create(DemoConfig.class).configWeb(builder -> {
            builder.addWebSocketEndpoint("com.yijvyan.websocket.MyWebSocket");
        }).start();

        // 13.2.jfinal_undertow 支持 jsp 功能需要类似下面的配置；
        /*UndertowServer.create(DemoConfig.class).configWeb(wb -> {
            wb.getDeploymentInfo().addServlet(JspServletBuilder.createServlet("Default Jsp Servlet", "*.jsp"));
            HashMap<String, TagLibraryInfo> tagLibraryInfo = new HashMap<String, TagLibraryInfo>();
            JspServletBuilder.setupDeployment(wb.getDeploymentInfo(), new HashMap<String, JspPropertyGroup>(),
                tagLibraryInfo, new HackInstanceManager());
        }).start();*/

        System.out.println("13.1.websocket：系统服务启动完成......");
    }

    @Override
    public void configConstant(Constants me) {
        me.setDevMode(true);
        // 13.1.配置在浏览器地址栏中无法输入 .jsp 文件名去访问 jsp 文件；设为false后，可以直接访问；
        // me.setDenyAccessJsp(false);

    }

    @Override
    public void configRoute(Routes me) {
        me.add("/hello", HelloController.class);
    }

    @Override
    public void configEngine(Engine me) {

    }

    @Override
    public void configPlugin(Plugins me) {

    }

    @Override
    public void configInterceptor(Interceptors me) {

    }

    @Override
    public void configHandler(Handlers me) {
        // 13.2.不拦截websocket；
        // me.add(new UrlSkipHandler("^/myapp.ws", false));
    }

}
